package com.heima.search.service.impl;

import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.HistorySearchDto;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.utils.threadlocal.AppThreadLocalUtils;
import com.heima.search.service.ApUserSearchService;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ApUserSearchServiceimpl implements ApUserSearchService {
    @Resource
    MongoTemplate mongoTemplate;

    @Override
    @Async("taskExecutor")
    public void insert(UserSearchDto dto) {
        //参数校验
        if (dto == null || StringUtils.isBlank(dto.getSearchWords())) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //查询搜索记录是否存在
        ApUserSearch userSearch = mongoTemplate.findOne(Query.query(Criteria.where("entryId").is(dto.getEquipmentId()).and("keyword").is(dto.getSearchWords())), ApUserSearch.class);
        if (userSearch == null) {

            //封装添加数据
            ApUserSearch apUserSearch = new ApUserSearch();
            apUserSearch.setCreatedTime(new Date());
            apUserSearch.setEntryId(dto.getEquipmentId());
            apUserSearch.setKeyword(dto.getSearchWords());

            //添加
            mongoTemplate.save(apUserSearch);
        } else {
            userSearch.setCreatedTime(new Date());
            mongoTemplate.save(userSearch);
        }

    }

    @Override
    public ResponseResult findUserSearch(UserSearchDto dto) {
        //参数校验
        if (dto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        if (dto.getPageSize() == 0) {
            dto.setPageSize(10);
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }

        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }

        if (dto.getEntryId() == null) {
            dto.setEntryId(user.getId());
        }

        //查询当前用户的全部搜索历史
        List<ApUserSearch> apUserSearches = mongoTemplate.find(Query.query(Criteria.where("entryId").is(dto.getEntryId())).with(Sort.by(Sort.Order.desc("createdTime"))).limit(dto.getPageSize()), ApUserSearch.class);
        //返回数据
        return ResponseResult.okResult(apUserSearches);
    }

    @Override
    public ResponseResult delUserSearch(HistorySearchDto dto) {
        //参数校验
        if (dto == null || dto.getId() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //获取当前登录用户
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }

        if (dto.getEquipmentId() == null) {
            dto.setEquipmentId(user.getId());
        }

        //删除搜索历史
        mongoTemplate.remove(Query.query(Criteria.where("id").is(dto.getId()).and("entryId").is(dto.getEquipmentId())), ApUserSearch.class);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
